package com.electric.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.electric.common.CommonStringUtils;
import com.electric.common.Contains;
import com.electric.common.LoggerInterface;
import com.electric.entity.RepairType;
import com.electric.entity.User;
import com.electric.entity.VoltageLevel;
import com.electric.service.IRepairTypeService;
import com.electric.service.IVoltageLevelService;

/**
 * <p>
 * 故障记录 前端控制器
 * </p>
 *
 * @author zhangbin
 * @since 2018-08-28
 */
@Controller
public class InitController implements LoggerInterface{
	@Autowired
	private IVoltageLevelService voltageLevelService;
	@Autowired
	private IRepairTypeService repairTypeService;

	@RequestMapping("/init")
	public ModelAndView init() {
		ModelAndView view = new ModelAndView();
		
		// 建立故障类型
		List<RepairType> insertRepairTypeList = new ArrayList<RepairType>();
		
		RepairType repairType = new RepairType();
		repairType.setCode(Contains.VOLTAGE_LOSS_CODE);
		repairType.setRepairType("失压");
		repairType.setSeqId(CommonStringUtils.getUUID());
		insertRepairTypeList.add(repairType);
		
		RepairType repairType1 = new RepairType();
		repairType1.setCode("FLOW_LOSS");
		repairType1.setRepairType("失流");
		repairType1.setSeqId(CommonStringUtils.getUUID());
		insertRepairTypeList.add(repairType1);
		
		RepairType repairType2 = new RepairType();
		repairType2.setCode("REVERSE_ACTIVE_POWER_TAKING");
		repairType2.setRepairType("反向有功走字");
		repairType2.setSeqId(CommonStringUtils.getUUID());
		insertRepairTypeList.add(repairType2);
		
		RepairType repairType3 = new RepairType();
		repairType3.setCode("CURRENT_IMBALANCE");
		repairType3.setRepairType("电流不平衡");
		repairType3.setSeqId(CommonStringUtils.getUUID());
		insertRepairTypeList.add(repairType3);
		
		logger.debug(insertRepairTypeList);
		
		repairTypeService.insertBatch(insertRepairTypeList, Contains.BATCH_SIZE);
		
		// 建立电压等级
		List<VoltageLevel> insertVoltageLevel = new ArrayList<VoltageLevel>();
		
		VoltageLevel voltageLevel = new VoltageLevel();
		voltageLevel.setSeqId(CommonStringUtils.getUUID());
		voltageLevel.setValue(10);
		insertVoltageLevel.add(voltageLevel);
		
		VoltageLevel voltageLevel1 = new VoltageLevel();
		voltageLevel1.setSeqId(CommonStringUtils.getUUID());
		voltageLevel1.setValue(35);
		insertVoltageLevel.add(voltageLevel1);
		
		logger.debug(insertVoltageLevel);
		
		voltageLevelService.insertBatch(insertVoltageLevel,Contains.BATCH_SIZE);
		
		// 建立管理员账号
		User user = new User();
		user.setLoginName("admin");
		user.setLoginPwd("123");
		user.setUserId(CommonStringUtils.getUUID());
		user.setUserName("后台管理员");
		
		logger.debug(user);
		
		user.insert();
		
		return view;
	}
	
	/**
	 * 登录成功后的跳转页面
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("redirect:/statistics/init");
		return view;
	}
}

